package com.xyz.rok.tools.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyz.rok.tools.mapper.UserMapper;
import com.xyz.rok.tools.model.User;
import com.xyz.rok.tools.model.WxSubMsg;
import com.xyz.rok.tools.model.WxSubMsgBody;
import com.xyz.rok.tools.model.WxSubMsgBodyData;
import com.xyz.rok.tools.service.WxAppService;
import com.xyz.rok.tools.service.WxAppWrapperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/8/3 17:51
 **/
@Slf4j
@Service
public class WxAppWrapperServiceImpl implements WxAppWrapperService {

    public static ReentrantLock lock = new ReentrantLock();
    private final String CLIENT_CREDENTIAL = "client_credential";
    private final String TEMPLATE_ID = "QHwd_XCdVJzQC8VDnjPfUSGVc8pkSnRHmiXouenF58o";
    private final String TEMPLATE_ID2 = "bzJWiieK2DR4_Cg5tRGdK3-ssw7PZxJGNiNuIUc04Lo";
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
    @Autowired
    private WxAppService wxAppService;
    @Autowired
    private WxAppWrapperService wxAppWrapperService;
    private ObjectMapper mapper = new ObjectMapper();
    @Value("${weixin.appid:}")
    private String appId;
    @Value("${weixin.secret:}")
    private String secret;
    @Resource
    private UserMapper userMapper;

    @Override
    public void pushMsg(Long toUserId, String content, Date date) {
        User user = userMapper.selectByPrimaryKey(toUserId);
        if (user != null) {
            WxSubMsg wxSubMsg = new WxSubMsg();
            wxSubMsg.setTouser(user.getOpenId());
            wxSubMsg.setTemplateId(TEMPLATE_ID);
            WxSubMsgBody wxSubMsgBody = new WxSubMsgBody();
            wxSubMsgBody.setThing1(new WxSubMsgBodyData(content));
            wxSubMsgBody.setTime11(new WxSubMsgBodyData(sdf.format(new Date())));
            wxSubMsg.setData(wxSubMsgBody);
            String result = wxAppService.pushMsg(wxAppWrapperService.getAccessToken(), wxSubMsg);
            log.info(result);
        }

    }

    @Override
    public void pushQueueMsg(Long toUserId, Integer title) {
        User user = userMapper.selectByPrimaryKey(toUserId);
        if (user != null) {
            WxSubMsg wxSubMsg = new WxSubMsg();
            wxSubMsg.setTouser(user.getOpenId());
            wxSubMsg.setTemplateId(TEMPLATE_ID2);
            WxSubMsgBody wxSubMsgBody = new WxSubMsgBody();
            //0:公爵 1:科学家 2:建筑师
            if (title == 0) {
                wxSubMsgBody.setThing8(new WxSubMsgBodyData("公爵排队"));
            } else if (title == 1) {
                wxSubMsgBody.setThing8(new WxSubMsgBodyData("科学家排队"));
            } else {
                wxSubMsgBody.setThing8(new WxSubMsgBodyData("建筑师排队"));
            }
            wxSubMsgBody.setNumber6(new WxSubMsgBodyData("0"));
            wxSubMsg.setData(wxSubMsgBody);
            String result = wxAppService.pushMsg(wxAppWrapperService.getAccessToken(), wxSubMsg);
            log.info(result);
        }
    }

    @Override
    @Cacheable(value = "accessToken")
    public String getAccessToken() {
        lock.lock();
        try {
            Map<String, String> map = mapper.readValue(wxAppService.getAccessToken(appId, secret, CLIENT_CREDENTIAL), Map.class);
            return map.get("access_token");
        } catch (Exception e) {
            log.error("getAccessToken error", e);
        } finally {
            lock.unlock();
        }
        return null;
    }
}
